<?php
/** Tiehui.Lu 512701323@qq.com utf-8 下午03:27:24 2009-4-28 */
//session_start ();
//Log
//$writer4 = new Zend_Log_Writer_Null;//Zend_Log_Writer_Null是一个不向任何地方写入任何数据的存根. 用于在测试期间关闭或踩熄日志.
//$logger = new Zend_Log ($writer4 );
$logger = new Zend_Log ( );
$columnMapping = array ('user_id' => 'userId', 'user_name' => 'userName', 'priority' => 'priority', 'priorityName' => 'priorityName', 'msg' => 'message', 'ip' => 'ip', 'time' => 'time' );
global $db;
$writer = new Zend_Log_Writer_Db ( $db, DB_PRE . 'log', $columnMapping );
$writer2 = new Zend_Log_Writer_Stream ( LOG_DIR . date ( 'Ym' ) );
$writer3 = new Zend_Log_Writer_Stream ( 'php://output' );
$logger->addWriter ( $writer );
$logger->addWriter ( $writer2 );
//$logger->addWriter ( $writer3 );


$logger->addPriority ( 'INSERT', 1000 );
$logger->addPriority ( 'UPDATE', 1001 );
$logger->addPriority ( 'DELETE', 1002 );
$logger->addPriority ( 'LOGIN', 1003 );
$logger->addPriority ( 'LOGOUT', 1004 );

//Zend_Log_Formatter_Simple 是默认的.当未指定任何formatter时使用. 默认的配置等同于下面的配置
$format = '%userName%(%userId%) %ip% %timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple ( $format );
$writer2->setFormatter ( $formatter );
//$writer3->setFormatter ( $formatter );


$filter = new Zend_Log_Filter_Priority ( Zend_Log::DEBUG );
//$logger->addFilter($filter);
$writer2->addFilter ( $filter );

$logger->setEventItem ( 'userId', getLogUserId () );
$logger->setEventItem ( 'userName', getLogUserName () );
$logger->setEventItem ( 'ip', getLogIp () );
$logger->setEventItem ( 'time', getLogTime () );

function getLogUserId() {
	return isset($_SESSION ['user'] ['id'])?$_SESSION ['user'] ['id']:0;
}

function getLogUserName() {
	return isset($_SESSION ['user'] ['name'])?$_SESSION ['user'] ['name']:null;
}

function getLogIp() {
	return $_SERVER ['REMOTE_ADDR'];
}

function getLogTime() {
	return time ();
}

?>